<?php

namespace App\Http\Repository;

use App\Message;
use App\MessageField;
use Illuminate\Support\Facades\Schema;

class MessageRepository
{
    private $messageField;
    private $message;

    public function __construct(MessageField $messageField, Message $message)
    {
        $this->messageField = $messageField;
        $this->message = $message;
    }

    //得到尚未绑定的字段
    public function getNoBindField()
    {
        $tmpFields = Schema::getColumnListing('messages');
        $uesdFields = \App\MessageField::lists('field')->toArray();
        foreach ($tmpFields as $k => $field) {
            if (strpos($field, 'message_field_') === false) {
                unset($tmpFields[$k]);
            }
            if (array_search($field, $uesdFields) !== false) {
                unset($tmpFields[$k]);
            }
        }

        return $tmpFields;
    }

    //得到前台的在线留言列表信息
    public function getMessage($num = 10, $info = null)
    {
        $tmpMessages = $this->message->orderBy('status', 'DESC')
            ->where(function ($query) use ($info) {
                if ($info) {
                    $query->where('info', $info);
                }
            })
            ->orderBy('created_at', 'DESC')
            ->take($num)
            ->get()
            ->toArray();

        $messageFields = $this->messageField->where('show', 1)
            ->lists('name', 'field')
            ->toArray();

        $messages = [];
        foreach ($tmpMessages as $index => $message) {
            foreach ($message as $k => $v) {
                if (!empty($messageFields[$k])) {
                    $messages[$index]['data'][] = $messageFields[$k] . ' : ' . $v;
                } else {
                    if ($v) {
                        $messages[$index][$k] = $v;
                    }
                }
            }
        }
        return $messages;
    }

    //得到后台的在线留言列表信息
    public function getMessagForAdmin()
    {
        $tmpMessages = $this->message->orderBy('status', 'DESC')
            ->orderBy('created_at', 'DESC')
            ->get()
            ->toArray();

        $messageFields = $this->messageField->lists('name', 'field')->toArray();

        $messages = [];
        foreach ($tmpMessages as $index => $message) {
            foreach ($message as $k => $v) {
                if (!empty($messageFields[$k])) {
                    $messages[$index]['data'][] = $messageFields[$k] . ' : ' . $v;
                } else {
                    if ($v) {
                        $messages[$index][$k] = $v;
                    }
                }
            }
            $messages[$index]['info'] = \App\Info::select('chunks.path', 'infos.id', 'infos.name')
                ->join('info_sort_lines', 'info_sort_lines.info', '=', 'infos.id')
                ->join('chunks', 'chunks.id', '=', 'info_sort_lines.chunk')
                ->where('infos.id', $message['info'])
                ->first();
        }

        return $messages;
    }

    //得到未读在线留言数量
    public function getMessageNum()
    {
        return $this->message->where('status', 1)->count();
    }

    //得到在线留言的字段绑定值
    public function getMessageFields()
    {
        $messageFields = $this->messageField->lists('value', 'alias')->toArray();

        foreach ($messageFields as $alias => $value) {
            $messageFields[$alias] = explode(',', $value);
        }

        return $messageFields;
    }

    //得到每月的在线留言数量
    public function getMessageEveryMonth($year = '')
    {
        $year = !empty($year) ? $year : date('Y');

        $messages = \App\Message::get()->toArray();

        $data = [];
        foreach ($messages as $value) {
            $timeArray = explode('-', $value['created_at']);
            if ($timeArray[0] != $year) {
                continue;
            } else {
                !empty($data[(string)(int)$timeArray[1]]) ? $data[(string)(int)$timeArray[1]]++ : $data[(string)(int)$timeArray[1]] = 1;
            }
        }

        return $data;
    }
}